package com.lmx.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmx.project.common.ErrorCode;
import com.lmx.project.exception.BusinessException;
import com.lmx.project.model.dto.category.CategoryRequestUpdate;
import com.lmx.project.model.dto.users.UserExcle;
import com.lmx.project.model.entity.Category;
import com.lmx.project.model.entity.Users;
import com.lmx.project.service.CategoryService;
import com.lmx.project.mapper.CategoryMapper;
import com.lmx.project.service.UsersService;
import com.lmx.project.untils.ExcleUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Lenovo
 * @description 针对表【category(类别表)】的数据库操作Service实现
 * @createDate 2023-04-07 22:36:34
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {
    @Resource
    private UsersService usersService;

    @Resource
    private ExcleUtils excleUtils;

    private String SALT = "lmx";
    @Override
    @Transactional // 如果抛出异常回滚数据
    public boolean saveUserByExcle(MultipartFile contact) throws IOException {

        boolean excelFile = excleUtils.isExcelFile(contact);
        if (!excelFile) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请上传excle表格");
        }
//        读取表格内容
        List<UserExcle> userExcles = excleUtils.readExcel(contact.getInputStream(), UserExcle.class);

//        处理数据，保存到数据库中
        List<Category> list = list();

        HashMap<String, Long> typemap = new HashMap<>();

//    将列表处理为map的形式，便于查找
        list.forEach(type -> {
            typemap.put(type.getName(), type.getId());
        });

        ArrayList<Users> usersArrayList = new ArrayList<>();

        userExcles.forEach(userExcle -> {
            Users users = new Users();
            BeanUtils.copyProperties(userExcle, users);

            if (typemap.containsKey(users.getTypemc())) {
                users.setTypedm(typemap.get(users.getTypemc()));
            }
//            else {
////                说明此时的类别表里面没有数据
//                Type type = new Type();
//                type.setName(users.getTypemc());
//                boolean save = typeService.save(type);
//                if (save) {
//                    users.setTypedm(type.getId());
//                    typemap.put(type.getName(), type.getId());
//                }
//            }

            users.setPassword(
                    DigestUtils.md5DigestAsHex((SALT + "12345678").getBytes()));

            usersArrayList.add(users);


        });

        boolean b = usersService.saveBatch(usersArrayList);

        return b;
    }

    @Override
    @Transactional
    public Long updateCategory(CategoryRequestUpdate categoryRequestUpdate) {

        //        判断是否存在
        Category byId = getById(categoryRequestUpdate.getId());
        if (byId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该类别不存在");
        }
//        修改用户的信息
        Category category = new Category();
        BeanUtils.copyProperties(categoryRequestUpdate, category);
        boolean b = updateById(category);
//        修改用户信息中的所属列表
        final LambdaUpdateWrapper<Users> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Users::getTypemc, categoryRequestUpdate.getName()).eq(Users::getTypedm,
                categoryRequestUpdate.getId());
        usersService.update(updateWrapper);
        return category.getId();
    }
}




